/*
 *  Getris -- A Tetris game written in Groovy.
 *  Copyright (C) 2008 Johnny Jian
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package cn.johnnyjian.getris

import cn.johnnyjian.getris.game.color.ColorfulQuadrelUtils
import cn.johnnyjian.getris.panel.InfoPanel
import cn.johnnyjian.getris.panel.PreviewPanel
import cn.johnnyjian.getris.panel.TetrisPanel
import groovy.swing.SwingBuilder
import java.awt.event.KeyEvent
import javax.swing.ImageIcon
import javax.swing.JOptionPane
import javax.swing.UIManager
import javax.swing.WindowConstants

final aboutMessage = '''\
Getris -- A Tetris game written in Groovy
Copyright (C) 2008 Johnny Jian

Author: Johnny Jian <johnnyjianhy@gmail.com>\
'''

final setPauseText = { text, key ->
	pause*.text = text
	pause*.mnemonic = key
}
final setPauseEnabled = { enabled ->
	pause*.enabled = enabled
}
final setToResume = setPauseText.curry('Resume', KeyEvent.VK_R)
final setToPause = setPauseText.curry('Pause', KeyEvent.VK_P)
final enablePause = setPauseEnabled.curry(true)
final disablePause = setPauseEnabled.curry(false)

final exitAction = {
	System.exit(0)
}
final aboutAction = {
	JOptionPane.showMessageDialog(frame, aboutMessage, 'About', JOptionPane.PLAIN_MESSAGE)
}
final newGameAction = {
	tetris.newGame()
	setToPause()
	enablePause()
}
final pauseAction = {
	if (tetris.running) {
		tetris.pause()
		setToResume()
	}
}
final pauseOrResumeAction = {
	if (tetris.running) {
		tetris.pause()
		setToResume()
	} else if (tetris.paused) {
		tetris.resume()
		setToPause()
	}
}

UIManager.lookAndFeel = UIManager.systemLookAndFeelClassName
ColorfulQuadrelUtils.registerColorfulQuadrels()

pause = []
swing = new SwingBuilder()
frame = swing.frame(
		title: 'Getris',
		size: [370, 460], // XXX: should it be refractored to final variables (also to the other constants) ?
		layout: null, // set for absolute localization. XXX: change to more flexible layout
		locationRelativeTo: null, // set the window to the center of the screen
		resizable: false,
		defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE,
		windowLostFocus: pauseAction,
		iconImage: new ImageIcon(getClass().getResource('/icon/getris.gif')).image
) {
	menuBar {
		menu(text: 'Game', mnemonic: KeyEvent.VK_G) {
			menuItem(text: 'New Game', mnemonic: KeyEvent.VK_N, actionPerformed: newGameAction)
			pause << menuItem(text: 'Pause', mnemonic: KeyEvent.VK_P, actionPerformed: pauseOrResumeAction, enabled: false)
			separator()
			menuItem(text: 'Exit', mnemonic: KeyEvent.VK_E, actionPerformed: exitAction)
		}
		menu(text: 'Help', mnemonic: KeyEvent.VK_H) {
			menuItem(text: 'About', mnemonic: KeyEvent.VK_A, actionPerformed: aboutAction)
		}
	}
	tetris = panel(new TetrisPanel(10, 20, 20, 1), location: [20, 0])
	panel(new PreviewPanel(5, 4, 20, tetris), location: [240, 0])
	panel(new InfoPanel(tetris), size: [102, 122], location: [240, 100])
	button(text: 'New Game', mnemonic: KeyEvent.VK_N, location: [240, 290], size: [102, 30], actionPerformed: newGameAction)
	pause << button(
		text: 'Pause', mnemonic: KeyEvent.VK_P, location: [240, 330], size: [102, 30], actionPerformed: pauseOrResumeAction, enabled: false
	)
	button(text: 'Exit', mnemonic: KeyEvent.VK_E, location: [240, 370], size: [102, 30], actionPerformed: exitAction)
}
tetris.addObserver({ o, game ->
	if (game.container.gameOver) {
		disablePause()
	}
} as Observer)

frame.visible = true
